package com.android.list;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

public class AddItemToListActivity extends Activity {

	ListView list = null;
	AddTailAdapter adapter = null;
	Button btnAdd = null;
	ProgressBar progessBar = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		list = (ListView) findViewById(R.id.mainList);
		adapter = new AddTailAdapter(this);

		LayoutInflater mInflate = getLayoutInflater();
		list.addFooterView(mInflate.inflate(R.layout.add, null));
		list.setAdapter(adapter);

		btnAdd = (Button) list.findViewById(R.id.btnAdd);
		btnAdd.setOnClickListener(new AddListener());
		
		progessBar = ((ProgressBar) list.findViewById(R.id.progressBar));
		progessBar.setVisibility(View.INVISIBLE);
		
	}

	public class AddListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			new AsyncTask<Void, Void, Void>() {

				@Override
				protected Void doInBackground(Void... params) {
					// TODO Auto-generated method stub
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						Log.i(this.getClass().getName(), e.getMessage());
					}
					return null;
				}
				
				@Override
				protected void onPostExecute(Void result) {
					progessBar.setVisibility(View.INVISIBLE);
					runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							adapter.addTail();
						}
					});
				}
				
				@Override
				protected void onPreExecute() {
					progessBar.setVisibility(View.VISIBLE);
				}
			}.execute();
		}
	}
}
